<template>
  <div class="address">
    <van-nav-bar
      title="地址管理"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    fixed/>

    <div class="addresslist">
      <van-swipe-cell v-for="item in list" :key="item.id">
        <div class="list">
          <div @click="goOrder(item)">
            <div class="name">{{ item.name }}</div>
            <div class="number">
              <p class="mobile">{{ item.mobile }}</p>
              <p class="address2">{{ item.address }}</p>
              <p class="detail">{{ item.address_detail }}</p>
            </div>
          </div>
          <div>
            <van-icon
              name="edit"
              class="edit"
              size="40px"
              @click="goEdit(item.id)"
            />
          </div>
        </div>
        <template #right>
          <van-button
            square
            type="danger"
            text="删除"
            class="del"
            @click="delAddress(item.id)"
          />
        </template>
      </van-swipe-cell>
    </div>

    <div class="fixbox">
      <div class="bulit" @click="add">+ 新建地址</div>
    </div>
  </div>
</template>

<script>
import { getListAction,deleteAction } from "@/api/cart/order/address";
export default {
  data() {
    return {
      list: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    init() {
      getListAction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
        this.list = res.data;
      });
    },
    goOrder(address) {
      localStorage.setItem("addressInfo", JSON.stringify(address));
      this.$router.go(-1);
    },
    goEdit(id) {
      this.$router.push(`/cart/order/address/edit/${id}`);
      //   console.log(id);
      localStorage.setItem("add", 1);
    },
    add() {
      this.$router.push("/cart/order/address/addAddress");
      localStorage.setItem("add", 0);
    },
    delAddress(id) {
      deleteAction({
        id,
      }).then((res) => {
        console.log(res);
        this.init();
      });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style lang="scss" scoped>
.address {
  background-color: rgb(242, 242, 242);
  .addresslist {
    width: 710px;
    background-color: #fff;

    // height: 2000px;
    margin: 90px auto 158px;
    .del {
      height: 100%;
    }
    .list {
      height: 228px;
      border: 1px solid rgba(223, 223, 223, 1);
      position: relative;
      .name {
        font-size: 30px;
        color: #333333;
        letter-spacing: 0;
        font-weight: 600;
        margin: 49px 21px 0px 34px;
      }
      .number {
        font-size: 28px;
        color: #999999;
        letter-spacing: 0;
        font-weight: 400;
        .mobile {
          position: absolute;
          left: 155px;
          top: 55px;
          font-size: 28px;
          color: #333333;
          letter-spacing: 0;
          font-weight: 400;
        }
        .address2 {
          position: absolute;
          left: 155px;
          top: 111.5px;
        }
        .detail {
          position: absolute;
          left: 155px;
          bottom: 36px;
        }
      }
      .edit {
        position: absolute;
        right: 51px;
        top: 109px;
      }
    }
  }
  .fixbox {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 190px;
    background-color: #fff;
    .bulit {
      position: fixed;
      bottom: 68px;
      left: 21px;
      width: 710px;
      height: 90px;
      background: #cea62a;
      border-radius: 44.5px;
      font-size: 30px;
      color: #ffffff;
      letter-spacing: 0;
      font-weight: 500;
      text-align: center;
      line-height: 90px;
    }
  }
}
</style>